import time
from typing import Annotated

import asyncio
from fastapi import APIRouter, Request, Depends
from fastapi.params import Header
from starlette.responses import Response

from app_v2.bili_api.api import ApiSession
from app_v2.dependencys import get_redis
from app_v2.middleware import get_stats
from app_v2.model import RedisPut

test = APIRouter(prefix="/test", tags=["test"])


@test.get("/ping")
async def ping(response: Response, x_request_start_time: Annotated[str | None, Header()] = None):
    return {"ping": "pong"}


@test.get("/async")
async def async_test():
    cookie = """SESSDATA=2544c61e%2C1767278850%2C044b9%2A72CjCu-sVcKI3iIzhz0Nbgx-LL5qabtoOhDDSV652iTXhaRtyGmhDLhXhWEuJe1dRroFsSVjdKbnFYYnZRZms3ZDVubkcyS3BWX0hBeDBPYlZxWUloaldacTY2a21obEtaRFllNDB4VnFCeWZMaG9zU2JBeUJjbTNCV0E1RllSYVozZ0hyQmkyV2lBIIEC; Path=/; Domain=bilibili.com; Expires=Thu, 01 Jan 2026 14:47:30 GMT; HttpOnly; Secure"""
    print(cookie)
    async with ApiSession(cookie) as session:
        return await session.up(2).up_word_cloud()


@test.put("/redis")
async def index(request: Request, rp: RedisPut):
    result = await request.app.state.redis.set(rp.key, rp.value)
    return {"code": 200 if result else 400}


@test.get("/redis")
async def redis(rq: Request, key: str):
    return {"result": await rq.app.state.redis.get(key)}


@test.get("/redis/get")
async def get_redis(rq: Request, key: str, redis=Depends(get_redis)):
    return {"result": await redis.get(key)}


@test.get("/stats")
async def stats():
    return {
        "code": 200,
        "data": get_stats()
    }
